package testJdk.commonUse.collections.map;

import cn.hutool.core.map.MapUtil;
import com.google.common.collect.Lists;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Name : learn_jdk.commonUse.collections.map.TestMap
 * @Description : 描述
 * Created by cyd on 2021-05-25 14:56:53
 */
public class TestMap {

    @Test
    public void test() {
        HashMap<String, String> map = new HashMap<>();
        map.put("1", "朱湖");
        map.put("2", "鹏兴");

        String s = MapUtil.sortJoin(map, ",", ":", false);
        System.out.println(s);

    }

    @Test
    public void testReplace() {
        HashMap<String, String> map = new HashMap<>();
        map.put("1", "朱湖");
        map.put("2", "鹏兴");
        System.out.println(map);
        map.put("1", "hhh");
        System.out.println(map);
    }

    @Test
    public void testParseStrToMap() {
        String s = "{tenantId=1002}";
        Map<String, Object> value = parseStrToMap(s);
        HashMap<String, String> hashMap = new HashMap<String, String>();
        value.entrySet().forEach(e -> {
            hashMap.put(e.getKey(), e.getValue().toString());
        });
        System.out.println(hashMap);
    }

    /**
     * 将字符串形式的map转为实际的map
     *
     * @param param 字符串形式的map
     * @return 实际的map
     */
    public static Map<String, Object> parseStrToMap(String param) {
        Map<String, Object> map = new HashMap<String, Object>();
        String str = "";
        String key = "";
        Object value = "";
        char[] charList = param.toCharArray();
        boolean valueBegin = false;
        for (int i = 0; i < charList.length; i++) {
            char c = charList[i];
            if (c == '{') {
                if (valueBegin == true) {
                    value = parseStrToMap(param.substring(i, param.length()));
                    i = param.indexOf('}', i) + 1;
                    map.put(key, value);
                }
            } else if (c == '=') {
                valueBegin = true;
                key = str;
                str = "";
            } else if (c == ',') {
                valueBegin = false;
                value = str;
                str = "";
                map.put(key, value);
            } else if (c == '}') {
                if (str != "") {
                    value = str;
                }
                map.put(key, value);
                return map;
            } else if (c != ' ') {
                str += c;
            }
        }
        return map;
    }


    /**
     * 将map转化为list
     */
    @Test
    public void testMapToList() {
        Map<String, String> map = new LinkedHashMap<>();

        map.put("3", "鹏兴");
        map.put("2", "xxx");
        map.put("1", "朱湖");

        ArrayList<Map.Entry<String, String>> list = Lists.newArrayList(map.entrySet());
        System.out.println(list);

        Map.Entry<String, String> entry = list.get(0);
        String value = entry.getValue();
        System.out.println(value);

        ArrayList<String> strings = Lists.newArrayList(map.values());
        System.out.println(strings);
    }

    /**
     * map切割
     */
    @Test
    public void test03() {
        // 用于计算的tendency
        Map<String, Long> tendency = new LinkedHashMap<>();
        tendency.put("1", 1L);
        tendency.put("2", 2L);
        tendency.put("3", 3L);
        tendency.put("4", 4L);

        System.out.println(tendency);
        HashMap<String, Long> tendency2 = new LinkedHashMap<>();

        // 只放3个
        for (Map.Entry<String, Long> next : tendency.entrySet()) {
            if (tendency2.size() <= 2) {
                tendency2.put(next.getKey(), next.getValue());
            }
        }

        System.out.println(tendency2);
    }
}
